#include "stdafx.h"
#include "Utils.hpp"

namespace he
{
	std::int_least32_t Convert::to_int32(char const *pString)
	{
		static_assert(sizeof(std::int_least32_t) >= sizeof(long), "to_int32() - unsigned long is larger than std::uint_least32_t");
		char *p_stop;
		long const number = strtol(pString, &p_stop, 10);
		// No conversion occurs
		if(pString == p_stop || errno == ERANGE)
			throw std::runtime_error(std::string("to_int32(): Cannot convert '") + pString + "' to number.");

		return number;
	}

	std::uint_least32_t Convert::to_uint32(char const *pString)
	{
		static_assert(sizeof(std::uint_least32_t) >= sizeof(unsigned long), "to_uint32() - unsigned long is larger than std::uint_least32_t");
		char *p_stop;
		unsigned long const number = strtoul(pString, &p_stop, 10);
		// No conversion occurs
		if(pString == p_stop || errno == ERANGE)
			throw std::runtime_error(std::string("Cannot convert '") + pString + "' to number.");

		return number;
	}

	double Convert::to_double(char const *pString)
	{
		char *p_stop;
		// Create C locale for parsing floats
		_locale_t locale = _create_locale(LC_ALL, "C");
		return _strtod_l(pString, &p_stop, locale);
	}
}